<?php

require_once '../../Lib/MyCache.php';

define('DS',  DIRECTORY_SEPARATOR);
define('DAY', 60*60*24);

$f = $_GET['f'];
$w = @$_GET['w'];
$h = @$_GET['h'];

$base = dirname(__FILE__);
$path = $base . "/" . $f;

if (! is_file($path)) {
    $path = dirname(__FILE__) . '/noimage.png';
}
$ext  = strtolower(substr($path, strrpos($path, ".")+1));

$key = $f . (isset($w) ? "_w{$w}" : '') . (isset($h) ? "_h{$h}" : '');
$img = MyCache::read($key);
if (! $img) {

    $img = new Imagick($path);
    $geo = $img->getImageGeometry();
    $w0 = $geo['width'];
    $h0 = $geo['height'];

    if ($w || $h) {
    	if ($w && $h) {
    		$img->scaleimage($w, $h, true);
    	} else {
    		$img->scaleimage($w, $h);
    	}
    }

    MyCache::write($key, $img);
}

header("Content-Type: image/{$ext}");
echo $img;
$img->destroy();

?>